﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using DevExpress.XtraSplashScreen;

namespace DevExpress.MailClient.Win.Controls.File_tabs
{
    public partial class ucFilePhysicalViewer : PRO_DOCS.BLL.WIN.Templates.DevxPermissionUserControlTemplate
    {
        public void DOWNLOAD_SELECTED_FILE()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "Document downloading.";
            sfd.FileName = CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NAME + CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION;
            sfd.DefaultExt = CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION;
            sfd.Filter = "*" + CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION + " | *." + CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION;
            if (sfd.ShowDialog().Equals(DialogResult.OK))
            {
                try
                {
                    new FileInfo(SELECTED_FILE_PATH).CopyTo(sfd.FileName, true);
                    if (XtraMessageBox.Show(string.Format("The current file has been downloaded to the selected path \"{0}\".{1}Do you want to open it.", sfd.FileName, Environment.NewLine), "Download completed", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2).Equals(DialogResult.OK))
                    {
                        System.Diagnostics.Process.Start(sfd.FileName);
                    }
                }
                catch { }


            }
        }

        private string SELECTED_FILE_PATH
        { get; set; }

        internal bool PHYSICAL_FILE_FOUND = false;

        public Dictionary<string, int> FILE_EXTENTION_ASSIGNED_GROUPS
        {
            get
            {
                Dictionary<string, int> fILE_EXTENTION_ASSIGNED_GROUPS = PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILE_CATEGORY_EXTENSIONs.ToDictionary(k => k.ARCHIVE_DEPARTMENT_FILE_CATEGORY_EXTENSION1.ToLower(), v => v.ARCHIVE_DEPARTMENT_FILE_CATEGORY_ID.Value);
                return fILE_EXTENTION_ASSIGNED_GROUPS;
            }
        }

        bool _cONTINUE_UPLOADING;
        bool CONTINUE_UPLOADING
        {
            get { return _cONTINUE_UPLOADING; }
            set
            {
                _cONTINUE_UPLOADING = value;
            }
        }

        private PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY;
        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY
        {
            get { return _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY; }
            set
            {
                _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY = value;
                tblFileDownload.Visible = false;

                pnlViewerControl.Controls.Clear();

                if (_cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY != null)
                {


                    if (!_cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID.Equals(Guid.Empty))
                    {
                        CURRENT_SELECTED_ARCHIVE_DEPARTMENT = _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENT;

                        for (int i = 0; i < CURRENT_ARCHIVES_COPYING_FOLDERS.Count; i++)
                        {
                            PHYSICAL_FILE_FOUND = new FileInfo(Path.Combine(CURRENT_ARCHIVES_COPYING_FOLDERS[i], _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID.ToString())).Exists;
                            if (PHYSICAL_FILE_FOUND)
                            {
                                SELECTED_FILE_PATH = Path.Combine(CURRENT_ARCHIVES_COPYING_FOLDERS[i], _cURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID.ToString());
                                break;
                            }
                        }
                        if (!PHYSICAL_FILE_FOUND)
                        {

                            tblFileDownload.Visible = true;
                            // XtraMessageBox.Show("The selected file was not found, please contact your Administratior and make sure you're connected to the saving directory", "I/O Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            lblErrorMessage.Text = "The selected file was not found, please contact your Administratior and make sure you're connected to the saving directory";
                        }
                        else
                        {

                            // SplashScreenManager.ShowForm(this.ParentForm, typeof(DevExpress.MailClient.Win.Forms.wfMain), false, true);

                            FileInfo fiSelectedFile = new FileInfo(CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID.ToString());

                            string fileExtension = CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION;

                            SHOW_FILE_DISPLAY(fileExtension, SELECTED_FILE_PATH);

                            tblFilesPreviewPage.Update();

                            //SplashScreenManager.CloseForm();
                        }
                    }
                    else
                    {
                        pnlViewerControl.Controls.Clear();
                    }


                }

            }
        }

        public void SHOW_FILE_DISPLAY(string fileExtension, string sELECTED_FILE_PATH)
        {
            fileExtension = fileExtension.ToLower();
            if (FILE_EXTENTION_ASSIGNED_GROUPS.ContainsKey(fileExtension))
            {
                CURRENT_SELECTED_VIEWER = (ApplicationViewers)FILE_EXTENTION_ASSIGNED_GROUPS[fileExtension];

                Control ctrlCURRENT_VIEWER = null;
                pnlViewerControl.Controls.Clear();

                switch (CURRENT_SELECTED_VIEWER)
                {
                    #region ADOBE_PDF_FILE & IMAGES_FILE
                    case ApplicationViewers.PDFFiles:
                        {
                            File_tabs.File_Viewers.ucPdfFileViewer ucPdfFileViewer = new File_Viewers.ucPdfFileViewer(sELECTED_FILE_PATH);
                            ctrlCURRENT_VIEWER = ucPdfFileViewer;
                        }
                        break;
                    case ApplicationViewers.ImagesFiles:
                        {
                            File_tabs.File_Viewers.ucImageFileViewer ucImageFileViewer = new File_Viewers.ucImageFileViewer(sELECTED_FILE_PATH);
                            ctrlCURRENT_VIEWER = ucImageFileViewer;
                        }
                        break;
                    #endregion

                    #region MICROSOFT_WORD_FILE
                    case ApplicationViewers.MicrosoftWordFiles:
                        {
                            File_tabs.File_Viewers.ucWordFileViewer ucWordFileViewer = new File_Viewers.ucWordFileViewer(sELECTED_FILE_PATH, fileExtension);
                            ctrlCURRENT_VIEWER = ucWordFileViewer;
                        }
                        break;
                    #endregion

                    #region MICROSOFT_EXCELL_FILE
                    case ApplicationViewers.MicrosoftExcellFiles:
                        {
                            File_tabs.File_Viewers.ucExcellFileViewer ucExcellFileViewer = new File_Viewers.ucExcellFileViewer(sELECTED_FILE_PATH, fileExtension);
                            ctrlCURRENT_VIEWER = ucExcellFileViewer;
                        }
                        break;
                    #endregion

                    #region SEGY Viewer
                    case ApplicationViewers.SgyViewer:
                        {
                            File_tabs.File_Viewers.ucSgyViewer ucSgyViewer = new File_Viewers.ucSgyViewer(sELECTED_FILE_PATH, fileExtension);
                            ctrlCURRENT_VIEWER = ucSgyViewer;
                        }
                        break;
                    #endregion

                    #region Tiff File Viewer
                    case ApplicationViewers.TiffViewer:
                        {
                            File_tabs.File_Viewers.ucTiffFileViewer ucTiffFileViewer = new File_Viewers.ucTiffFileViewer(sELECTED_FILE_PATH);
                            ctrlCURRENT_VIEWER = ucTiffFileViewer;
                        }
                        break;
                    #endregion
                    #region Las File Viewer
                    case ApplicationViewers.LasFileViewer:
                        {
                            File_tabs.File_Viewers.ucLasFileViewer ucLasFileViewer = new File_Viewers.ucLasFileViewer(sELECTED_FILE_PATH, fileExtension);
                            ctrlCURRENT_VIEWER = ucLasFileViewer;
                        }
                        break;
                    #endregion

                    #region Las File Viewer
                    case ApplicationViewers.MapsFileViewer:
                        {
                            File_tabs.File_Viewers.ucMapFileViewer ucMapFileViewer = new File_Viewers.ucMapFileViewer(sELECTED_FILE_PATH, fileExtension);
                            ctrlCURRENT_VIEWER = ucMapFileViewer;
                        }
                        break;
                    #endregion
                }
                ctrlCURRENT_VIEWER.Dock = DockStyle.Fill;
                pnlViewerControl.Controls.Add(ctrlCURRENT_VIEWER);
            }
            else
            {
                lblErrorMessage.Text = "The system does not support the current file, if you like to review the file please download it on your hard disk";
                tblFileDownload.Visible = true;
            }
        }

        PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT _cURRENT_SELECTED_ARCHIVE_DEPARTMENT;
        PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT CURRENT_SELECTED_ARCHIVE_DEPARTMENT
        {
            get { return _cURRENT_SELECTED_ARCHIVE_DEPARTMENT; }
            set
            {
                _cURRENT_SELECTED_ARCHIVE_DEPARTMENT = value;
                CURRENT_ARCHIVES_COPYING_FOLDERS = new List<string>();
                CONTINUE_UPLOADING = false;

                if (_cURRENT_SELECTED_ARCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENT_ID != 0)
                {
                    List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE> aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES = _cURRENT_SELECTED_ARCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHEs.ToList();

                    for (int i = 0; i < aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES.Count; i++)
                    {
                        if (new DirectoryInfo(aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES[i].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH).Exists)
                        {
                            CONTINUE_UPLOADING = true;
                            CURRENT_ARCHIVES_COPYING_FOLDERS.Add(aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES[i].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH);
                        }
                    }

                    if (!CONTINUE_UPLOADING)
                    {
                        XtraMessageBox.Show
                            (
                                string.Format("The storage media for the \"{0}\" department was not found.{1}Please make sure that you are connected to the network or contact your network administrator", _cURRENT_SELECTED_ARCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENT_NAME, Environment.NewLine),
                                "Storage Media was not found",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning
                            );
                    }


                }
            }
        }
        List<string> CURRENT_ARCHIVES_COPYING_FOLDERS
        {
            get;
            set;
        }

        private ApplicationViewers _cURRENT_SELECTED_VIEWER;
        private ApplicationViewers CURRENT_SELECTED_VIEWER
        {
            get { return _cURRENT_SELECTED_VIEWER; }
            set
            {
                if (value != null)
                {
                    _cURRENT_SELECTED_VIEWER = value;

                }
            }
        }

        private enum ApplicationViewers
        {
            PDFFiles = 1,
            MicrosoftWordFiles,
            MicrosoftExcellFiles,
            ImagesFiles,
            SgyViewer,
            LasFileViewer,
            MapsFileViewer,
            TiffViewer
        }

        public ucFilePhysicalViewer()
        {
            InitializeComponent();
        }
    }
}
